﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DataDisplaySaveFile : MonoBehaviour {

	//public int no; --> this guy refuses to allow value change, thanks to static variable in FileSaver
	private string _dataToPrint;

	public UILabel dataToPrint;

	public GameObject noData;
	public ExoSaveData savedFile;
	public ExoSaveData dataToOverwrite;
	public GameObject scrShotSprite;
	public Texture2D scrShotSpriteInTex;

	public ScreenshotData screenshotData;
	public BackLogData backLogData;
	
	string _fileDataPath = Application.persistentDataPath + "/SaveFile1";

	// Use this for initialization
	void Start () {

	}

	void Awake(){

		_fileDataPath = Application.persistentDataPath + "/SaveFile1";
		//inventory = GameObject.Find ("SampleInventoryUIHandler").GetComponent<SampleInventoryHandler>().playerInventory;
		//profiles = GameObject.Find ("SampleInventoryUIHandler").GetComponent<SampleInventoryHandler>().playerInventory; //Profiles. Subject to change.
		//flags = GameObject.FindGameObjectWithTag (Tags.gameEngine).GetComponent<FlagManager>().flagsBadEnds; //Flags. Subject to change.

		dataToOverwrite = new ExoSaveData();
		this.gameObject.SetActive(true);
		noData.gameObject.SetActive(false);
		Debug.Log (_fileDataPath);
		//this thing can't load Texture2D.
		savedFile = (ExoSaveData)SerializeUtility.LoadObject(_fileDataPath, typeof(ExoSaveData)); //--loads the file, but does not distribute it.
		Debug.Log (savedFile.DateOfSave);
		Debug.Log (savedFile.TimeOfSave);
		Debug.Log (savedFile.BackLog);
		Debug.Log("Save File 1 Loaded!");

		_dataToPrint += savedFile.DateOfSave + "\n" + savedFile.TimeOfSave + "\n" + "Chapter " + savedFile.currentChapter;
		dataToPrint = this.gameObject.GetComponentInChildren<UILabel>();
		dataToPrint.text = "";
		dataToPrint.text += _dataToPrint;

		byte[] ssInBytes = System.IO.File.ReadAllBytes(Application.persistentDataPath + "SaveFile1Photo");
		scrShotSpriteInTex.LoadImage(ssInBytes);
		scrShotSprite.GetComponent<SpriteRenderer>().sprite = Sprite.Create(scrShotSpriteInTex, scrShotSprite.GetComponent<SpriteRenderer>().sprite.rect, new Vector2(0.5f, 0.5f));
		scrShotSprite.GetComponent<SpriteRenderer>().sprite.name = "Save File Photo";
		scrShotSprite.transform.localScale = new Vector3(5.0f, 5.0f, 5.0f);
	}
	
	// Update is called once per frame
	void Update () {
		/*Texture2D tex = new Texture2D((int)scrShotSprite.GetComponent<SpriteRenderer>().sprite.rect.width, (int)scrShotSprite.GetComponent<SpriteRenderer>().sprite.rect.height);
		tex.LoadImage(savedFile.scrShotInBytes);
		scrShotSprite.GetComponent<SpriteRenderer>().sprite = Sprite.Create(tex, scrShotSprite.GetComponent<SpriteRenderer>().sprite.rect, new Vector2(0.5f, 0.5f));*/

		if(SerializeUtility.IsFileExist(_fileDataPath))
		{
			noData.SetActive(false);
			this.gameObject.SetActive(true);
			savedFile = (ExoSaveData)SerializeUtility.LoadObject(_fileDataPath, typeof(ExoSaveData));

			Debug.Log("Screenshot Loaded!");
		}
		else
		{
			this.gameObject.SetActive(false);
			noData.SetActive(true);
		}
	}

	public void overwriteSaveFile ()
	{
		dataToOverwrite.DateOfSave = System.DateTime.Now.ToShortDateString();
		dataToOverwrite.TimeOfSave = System.DateTime.Now.ToShortTimeString();
		int lastDia = PlayerPrefs.GetInt("LastDia");
		dataToOverwrite.currentChapter = lastDia;
		List<ExogenesisItem> inventory = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().playerInventory;
		List<int> inventoryIDs = new List<int>();
		for(int i = 0; i < inventory.Count; i++)
			inventoryIDs.Add(inventory[i].ID);
		dataToOverwrite.InventoryIDs = new List<int>(inventoryIDs);
		List<ProfileData> profiles = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles;
		List<int> profileIDs = new List<int>();
		for(int i = 0; i < profiles.Count; i++)
			profileIDs.Add(profiles[i].ID);
		dataToOverwrite.ProfileIDs = new List<int>(profileIDs);
		//dataToOverwrite.Flags.Add(flags[i]);
		//dataToOverwrite.BackLog = backLogData.allLines;
		//dataToOverwrite.scrShotInBytes = scrShotSpriteInTex.EncodeToJPG();

		SerializeUtility.SaveObject(_fileDataPath, dataToOverwrite);

		byte[] ssInBytes = screenshotData.getScreenshotInBytes();
		System.IO.File.WriteAllBytes(Application.persistentDataPath + "SaveFile1Photo", ssInBytes);
	}
}
